package fhp.hlhj.giantfold.activity.login
import android.app.AlertDialog
import android.content.Intent
import com.lzy.okgo.model.HttpParams
import fhp.hlhj.giantfold.BaseActivity
import fhp.hlhj.giantfold.Contents
import fhp.hlhj.giantfold.MainActivity
import fhp.hlhj.giantfold.R
import fhp.hlhj.giantfold.adapter.LoginPagerAdp
import fhp.hlhj.giantfold.interfaces.IAdv
import fhp.hlhj.giantfold.interfaces.IPhoneLogin
import fhp.hlhj.giantfold.interfaces.IWeChatLogin
import fhp.hlhj.giantfold.javaBean.AdvBean
import fhp.hlhj.giantfold.javaBean.PhoneLoginBean
import fhp.hlhj.giantfold.module.OtherLoginBean
import fhp.hlhj.giantfold.presenter.AdvPresenter
import fhp.hlhj.giantfold.presenter.PhoneLoginPresenter
import fhp.hlhj.giantfold.presenter.WechatLoginPresenter
import fhp.hlhj.giantfold.utils.LogUtil
import fhp.hlhj.giantfold.utils.MyUtils
import fhp.hlhj.giantfold.utils.SPUtils
import kotlinx.android.synthetic.main.activity_init_aty.*
import java.util.*

class InitAty : BaseActivity(),IPhoneLogin,IWeChatLogin,IAdv {
    private lateinit var wxLoginPresenter:WechatLoginPresenter
    private lateinit var phoneLoginPresenter:PhoneLoginPresenter
    private lateinit var advPresenter:AdvPresenter
    private lateinit var pagerAdp:LoginPagerAdp
    private val datas=ArrayList<AdvBean.DataBean>()
    val timer = Timer()
    val timeTask=object :TimerTask(){
        override fun run() {
            LogUtil.logi("开始自动登录${SPUtils.readLoginType(this@InitAty)}")
            when(SPUtils.readLoginType(this@InitAty)){
                0->{
                    val params = HttpParams()
                    params.put("user_name",SPUtils.readUserName(this@InitAty))
                    params.put("password",SPUtils.readUserPwd(this@InitAty))
                    if (MyUtils.isNetworkAvailable(this@InitAty)){
                        phoneLoginPresenter.login(params)
                    }else{
                        AlertDialog.Builder(this@InitAty).setTitle("没有检查到网络连接")
                                .setPositiveButton("确定"){
                                    dialog, which ->
                                    System.exit(0)
                                }
                                .show()
                    }
                }
                1->{

                    val httpParams = HttpParams()
                    httpParams.put("openid",SPUtils.readOpenId(this@InitAty))
                    httpParams.put("third_key",Contents.third_Key)
                    httpParams.put("login_type",0)
                    if (MyUtils.isNetworkAvailable(this@InitAty)){
                        wxLoginPresenter.loginWx(httpParams)

                    }else{
                        AlertDialog.Builder(this@InitAty).setTitle("没有检查到网络连接")
                                .setPositiveButton("确定"){
                                    dialog, which ->
                                    System.exit(0)
                                }
                                .show()
                    }
                }
                2->{
                    startActivity(Intent(this@InitAty,MainActivity::class.java))
                    finish()
                }
                -1->{
                    startActivity(Intent(this@InitAty,MainActivity::class.java))
                    finish()
                }
            }
        }

    }
    override fun getContentId(): Int = R.layout.activity_init_aty
    override fun initView() {
        wxLoginPresenter= WechatLoginPresenter(this)
        phoneLoginPresenter = PhoneLoginPresenter(this)
        advPresenter= AdvPresenter(this)


        pagerAdp= LoginPagerAdp(this,datas)
        viewPager.adapter=pagerAdp
        val advP = HttpParams()
        advP.put("block",5)
        if (MyUtils.isNetworkAvailable(this)){
            advPresenter.getAdv(advP)

        }else{
            AlertDialog.Builder(this@InitAty).setTitle("没有检查到网络连接")
                    .setPositiveButton("确定"){
                        dialog, which ->
                        System.exit(0)
                    }
                    .show()
        }



    }

    override fun initListener() {

        btSkip.setOnClickListener {
            timer.schedule(timeTask,0)
        }
    }
    override fun showLoading() {
    }

    /**
     * 手机登录
     */
    override fun login(phoneLoginBean: PhoneLoginBean) {
        if (phoneLoginBean.code==200){
            MyUtils.toast("登录成功")
            Contents.userInfo=phoneLoginBean.data
            Contents.userId=phoneLoginBean.data.id
            Contents.loginType=0
            startActivity(Intent(this,MainActivity::class.java))
            finish()
        }else{
            MyUtils.toast(phoneLoginBean.msg)
        }
    }
    /**
     * 微信登录
     */
    override fun login(otherLoginBean: OtherLoginBean) {
        if (otherLoginBean.code==200){
            Contents.userInfo=otherLoginBean.data
            Contents.userId=otherLoginBean.data.id
            LogUtil.logi("登录成功${Contents.userId}")
            Contents.loginType=1
            SPUtils.saveLoginType(this,1)
            startActivity(Intent(this,MainActivity::class.java))
            finish()
        }else{
            MyUtils.toast("登录失败")
        }
    }

    /**
     * 广告
     */
    override fun getADV(advBean: AdvBean) {
        if (advBean.code==200){
            datas.addAll(advBean.data)
            pagerAdp.notifyDataSetChanged()
        }
    }
}
